package week5.day31;

import org.junit.Test;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;

public class Demo1 {
    @Test
    public void test1() {
        // 1.存储一组学生。包括 姓名 年龄，总分，入学日期(Date)
        // 2.找出总分最高的学生？
        // 3.找出总分最低的学生
        // 4.求平均分
        // 5.删除低于平均分的学生
        // 6.找出2年内入学的学生。
        // 7.需要删除叫张三的学生
        // 8.删除年龄小于18的学生

        Collection<Student> students = generateStudentCollection();

        Student hightScoreStudent = findHighScoreStudent(students);

        System.out.println(hightScoreStudent);

        deleteLowAvgScoreStudent(students);

        students = generateStudentCollection();

        findEnterDateBetween2Years(students);

        System.out.println("deleteNameZsStudent(students) = " + deleteNameZsStudent(students));
        System.out.println("students = " + students);

        System.out.println("deleteAgeLow18Student(students) = " + deleteAgeLow18Student(students));
        System.out.println("students = " + students);
    }

    private boolean deleteAgeLow18Student(Collection<Student> students) {
        return students.removeIf(student -> student.getAge()<18);
    }

    private boolean deleteNameZsStudent(Collection<Student> students) {
        return students.removeIf(student -> "张三".equals(student.getName()));
    }

    private void findEnterDateBetween2Years(Collection<Student> students) {
        // 6.找出2年内入学的学生。 ===》 问清楚。
        // 2022-09-01
        System.out.println("两年内入学的学生：");
        Date currentDate = new Date();
        Iterator<Student> iterator = students.iterator();
        while (iterator.hasNext()){
            Student student = iterator.next();
            long timeDifference = currentDate.getTime() - student.getEnterDate().getTime();
            long twoYearsInMilliseconds = 2L * 365 * 24 * 60 * 60 * 1000;
            if (timeDifference <= twoYearsInMilliseconds) {
                System.out.println("student = " + student);
            }
        }
    }

    private void deleteLowAvgScoreStudent(Collection<Student> students) {

        // 算平均分。
        int avgScore = 50;

        // students.removeIf((student -> student.getScore() < avgScore));

        Iterator<Student> iterator = students.iterator();

        while (iterator.hasNext()) {
            Student student = iterator.next();
            if (student.getScore() < avgScore) {
                iterator.remove();
            }
        }

        System.out.println(students.size());
    }

    private Student findHighScoreStudent(Collection<Student> students) {

        int maxScore = -1;
        Student highScoreStudent = null;

        for (Student student : students) {
            if (student.getScore() > maxScore) {
                maxScore = student.getScore();
                highScoreStudent = student;
            }
        }

        return highScoreStudent;
    }

    private Collection<Student> generateStudentCollection() {
        Collection<Student> students = new ArrayList<>();

        long currentTimeMillis = System.currentTimeMillis();

        for (int i = 0; i < 50; i++) {
            Student student = new Student();
            student.setName("student_" + i);
            student.setAge(i);
            student.setScore(i * 2 + 1);

            student.setEnterDate(new Date(currentTimeMillis - 1000 * 3600 * 24 * 31 * 2 * i));

            students.add(student);
        }

        return students;
    }

}

class Student {
    // 姓名 年龄，总分，入学日期(Date)
    private String name;
    private int age;
    private int score;
    private Date enterDate;

    public Student() {
    }

    public Student(String name, int age, int score, Date enterDate) {
        this.name = name;
        this.age = age;
        this.score = score;
        this.enterDate = enterDate;
    }

    @Override
    public String toString() {
        return "Student{" + "name='" + name + '\'' + ", age=" + age + ", score=" + score + ", enterDate=" + enterDate + '}';
    }

    public String getName() {
        return name;
    }

    public Student setName(String name) {
        this.name = name;
        return this;
    }

    public int getAge() {
        return age;
    }

    public Student setAge(int age) {
        this.age = age;
        return this;
    }

    public int getScore() {
        return score;
    }

    public Student setScore(int score) {
        this.score = score;
        return this;
    }

    public Date getEnterDate() {
        return enterDate;
    }

    public Student setEnterDate(Date enterDate) {
        this.enterDate = enterDate;
        return this;
    }
}
